<!-- 新增编辑车辆 -->
<template>
  <div>
    <el-dialog
      title="新增车辆"
      :visible="dialogFormVisible"
      width="600px"

      @close="onclose"
    >
      <el-form
        ref="cardialogFormRef"
        :model="careditForm"
        :rules="carRules"
      >
        <el-form-item
          prop="truckTypeId"
          label="车辆类型"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="careditForm.truckTypeId"

            placeholder="请选择"
          >
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>

          </el-select></el-form-item>
        <el-form-item
          label="车牌号码"
          :label-width="formLabelWidth"
          prop="licensePlate"
        >
          <el-input
            v-model="careditForm.licensePlate"
            placeholder="请输入车牌号码"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="GPS设备ID"
          :label-width="formLabelWidth"
          prop="deviceGpsId"
        >
          <el-input
            v-model="careditForm.deviceGpsId"
            placeholder="请输入GPS设备ID"
          ></el-input>
        </el-form-item>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="onclose">取 消</el-button>
        <el-button
          type="primary"
          @click="submit"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { truckTypeCommonList } from '@/api/common'
import { truckTypeAdd } from '@/api/transit'
export default {
  name: 'VehicleAdd',
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    }

  },
  data() {
    return {
      form: [],
      formLabelWidth: '95px',
      list: this.cartype,
      careditForm: {
        truckTypeId: '',
        licensePlate: '',
        deviceGpsId: ''
      },
      carRules: {
        truckTypeId: [
          { required: true, message: '车辆类型不能为空', trigger: 'blur' }
        ],
        licensePlate: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: '请输入GPS设备id', trigger: 'blur' }

        ]
      }
    }
  },
  async mounted() {
    this.list = await (await truckTypeCommonList()).data
  },
  methods: {
    onclose() {
      this.$emit('update:dialog-form-visible', false)
      this.careditForm.truckTypeId = ''
      this.careditForm.licensePlate = ''
      this.careditForm.deviceGpsId = ''
    },
    async submit() {
      try {
        await this.$refs.cardialogFormRef.validate()
        await truckTypeAdd(this.careditForm)
        this.$message.success('添加成功')
        this.onclose()
      } catch (error) {
        this.$message.success('添加失败，请检查')
        console.log(error)
      }
    }

  }
}
</script>
<style scoped lang="scss">
/deep/ .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
</style>
